<?php
/*
分页类,用于各类分页中
@author:jackborwn
@time:2011-5-2
@version:2.0.4
*/

class pageDivid{
	
	/*数据源类型,sql查询语句,array数据*/
	private $sourceType = 'array';
	
	/*数据源*/
	private $source ='';
	
	/*记录总数*/
	private $resCount = 0;
	
	private $pageSize = 8;
	
	private $pageSum = 0;
	
	/*网站基本地址*/
	public $baseURL = 'javascript:';
	public $curPage = 1;
	
	/*当前可视范围的显示条数*/
	public $showNumSum = 10;
	
	/*链接中保存页码的参数，一般是get的参数*/
	public $pageName = 'page';
	
	/*分页相关参数*/
	public $pageParam = array();
	
	
	/*
	 * @param mixed $s 数据源
	 * @param int $size 页面大小 
	 */
	public function __construct($s,$size=8){
		
		if((is_string($s) && $s=='') || $s==null){
			
			return null;
		}
		
		if(is_string($s)){
			$this->sourceType = 'sql';
		}else if(is_array($s)){
			
			$this->sourceType = 'array';	
		}else{
			throw new Exception('错误的数据源!');	
		}
		
		if(intval($size)<=0){
			$size = 8;
		}

		$this->source = $s;
		$this->pageSize =$size;
		$this->curPage = $_GET[$this->pageName];

	}

	public function buildPager($link=false){
		
		if($this->source==''){
			return false;
		}
		
		if(!preg_match('/{page}/',$this->baseURL)){
			$this->baseURL = !preg_match('/\?(.+)/is',$this->baseURL) ? $this->baseURL.'?' : $this->baseURL.'&';
		}
		
		$resSum  = $this->getResSum();
		$pageSum = $this->getPageSum();
		$curPage = $this->getCurPage();
		$resArea = $this->getResArea();
		$numList = $this->getPageNumList();
		$links   = $this->getPageLink();
		
		
		$start = ($curPage-1)*$this->pageSize;
		if($this->sourceType == 'array'){
			
			$resource = $resSum >0 ? array_slice($this->source,$start,$this->pageSize): $this->resource;
		}else if($this->sourceType == 'sql'){
			
			global $con;
			if($link == false){
				$resource = $resSum >0 ? $con->getResArray($this->source." limit $start,".$this->pageSize): array();
			}else{
				$resource = $resSum >0 ? $con->query($this->source." limit $start,".$this->pageSize) : $con->query($this->source." limit 0");	
			}
		}
		
		global $ui;
		if($ui){
			$ui->assign('pagesum',$pageSum);
			$ui->assign('nextpage',$links['next_page']);
			$ui->assign('prepage',$links['pre_page']);
			$ui->assign('size',$this->pageSize);
			$ui->assign('pagelist',$numList);
			$ui->assign('curpage',$curPage);
			$ui->assign('pagearea',$resArea);
			$ui->assign('firstpage',$links['first_page']);
			$ui->assign('lastpage',$links['last_page']);
			$ui->assign('recordsum',$resSum);			
		}
		
		$this->pageParam['pagesum'] = $pageSum;
		$this->pageParam['nextpage']=$links['next_page'];
		$this->pageParam['prepage']=$links['pre_page'];
		$this->pageParam['size']=$this->pageSize;
		$this->pageParam['pagelist']=$numList;
		$this->pageParam['curpage']=$curPage;
		$this->pageParam['firstpage']=$links['first_page'];
		$this->pageParam['lastpage']=$links['last_page'];
		$this->pageParam['recordsum']=$resSum;			
		
		return $resource;
		
	}


	/*
	获取记录总数
	*/
	protected function getResSum(){
		
		global $con;
		if($this->sourceType == 'sql'){
			
			if(preg_match('/\s+union|group by\s+/is',$this->source)){
			
				$this->resCount = $con->recordCount($this->source);
				
			}else{
				$sumSql = preg_replace('/select\s*([\s\S]+)\s*from/i','select count(*) as resum from',$this->source);
				$this->resCount = $con->recordSum($sumSql);
			}
			
			
		}
		
		if($this->sourceType == 'array'){
			$this->resCount = count($this->source);
		}
		
		return $this->resCount;
	}
	
	/*
	获取当前信息
	*/
	protected function getCurPage(){
		
		$this->curPage=intval($this->curPage)<=0 || $this->curPage=='' || empty($this->curPage) ? '1':intval($this->curPage);
		$this->curPage=$this->curPage > $this->pageSum && $this->pageSum>0 ? $this->pageSum : $this->curPage;
		$this->curPage=$this->curPage > $this->pageSum && $this->pageSum==0 ? 1 : $this->curPage;		
		
		return $this->curPage;
	}
	
	protected function getPageNumList(){
		
		
		$end_show   = ($this->showNumSum + $this->curPage) >= $this->pageSum ? $this->pageSum : $this->showNumSum + $this->curPage;
		$start_show = $this->curPage - $this->showNumSum <= 0 ? 1 : $this->curPage - $this->showNumSum;
		
		$number = array();
		$isVirtulURL = preg_match('/{page}/',$this->baseURL);
		for($i=$start_show;$i<=$end_show;$i++)
		{
			if($isVirtulURL){
				$number[$i] = str_replace('{page}',$i,$this->baseURL);
			}else{
				$number[$i] = $this->baseURL.$this->pageName."=".$i;
			}
		}
		
		return $number;
	}
	
	/**
	获取当前页的记录范围
	**/
	protected function getResArea(){
		
	
		if($this->resCount>0 && $this->resCount % $this->pageSize==0 || ($this->curPage-1)*$this->pageSize+1+$this->pageSize<=$this->resCount){
			$number=(($this->curPage-1)*$this->pageSize+1)." - " .($this->pageSize*$this->curPage);
			$number=$this->pageSize == 1 ? ($this->pageSize*$this->curPage):(($this->curPage-1)*$this->pageSize+1)." - " .($this->pageSize*$this->curPage);
		}
		else if(($this->curPage-1)*$this->pageSize+1+$this->pageSize > $this->resCount)
		{
			$number=$this->resCount % $this->pageSize >1 ? (($this->curPage-1)*$this->pageSize+1)." - ".$this->resCount : $this->resCount;
		}
		
		return $number;
	}
	
	protected function getPageSum(){
		
		if($this->resCount % $this->pageSize==0)
		{
			$this->pageSum = $this->resCount/$this->pageSize;
		}
		else
		{
			$this->pageSum =(int)($this->resCount/$this->pageSize)+1;
		}
		
		return $this->pageSum;
	}
	
	/*
	获取分页链接
	*/
	function getPageLink(){
		
		$links = array();
		
		if(!preg_match('/{page}/',$this->baseURL)){
			$links['next_page']  = $this->pageSize*$this->curPage < $this->resCount ? ($this->baseURL).$this->pageName."=".($this->curPage+1) : "javascript:";
			$links['pre_page']   = $this->curPage > 1 ? ($this->baseURL).$this->pageName."=".($this->curPage-1) : "javascript:";
			$links['first_page'] = $this->pageSum > 1 ? ($this->baseURL).$this->pageName."=1" : "javascript:";
			$links['last_page']  = $this->pageSum > 1 ? ($this->baseURL).$this->pageName."=".$this->pageSum : 'javascript:';
		}else{
			$links['next_page']  = $this->pageSize*$this->curPage < $this->resCount ? str_replace('{page}',$this->curPage+1,$this->baseURL) : "javascript:";
			$links['pre_page']   = $this->curPage > 1 ? str_replace('{page}',$this->curPage-1,$this->baseURL) : "javascript:";
			$links['first_page'] = $this->pageSum > 1 ? str_replace('{page}',1,$this->baseURL) : "javascript:";
			$links['last_page']  = $this->pageSum > 1 ? str_replace('{page}',$this->pageSum,$this->baseURL) : 'javascript:';
		}
		return $links;
	
	}
	
	
}

?>